import Vue from 'vue'
import Vuex from '../vuex'

//持久化插件
//replaceState subscribe plugins
function persists(store){
    let local = localStorage.getItem('VUEX:STATE');
    if(local){
        store.replaceState(JSON.parse(local));
    }
    store.subscribe((mutation,state)=>{
        //只要频繁操作，就要考虑防抖和节流
        localStorage.setItem('VUEX:STATE',JSON.stringify(state));
    })
}
Vue.use(Vuex)

let store = new Vuex.Store({
  strict: true, //严格模式 只能通过mutation来更改
  plugins: [
     persists
  ],
  state: {
      age:20
  },
  getters :{
    getAge(state){
        console.log('getter执行')
        return state.age + 10
    }
  },
  mutations: { //同步更改
    changeAge(state,payload){
        state.age += payload;
    }
  },
  actions: { //异步更改
    changeAge({commit}){
        setTimeout(()=>{
            commit('changeAge',5 )
        },3000)
    }
  },
  modules: {
      a:{
          state: {
              c: 100
          },
          namespaced: true,
          mutations: {
            changeAge(state,payload){
                console.log('a')
            }
          }
      },
      b: {
          state: {
              d: 200
          },
          mutations: {
            changeAge(state,payload){
                console.log('b')
            }
          },
          modules: {
              c: {
                  state: {
                      ee: 3000
                  },
                  namespaced: true,
                  mutations: {
                    changeAge(state,payload){
                        console.log('b c')
                    }
                  },
              }
          }
      }
  }
})
store.registerModule(['e'],{
    state: {
        ff:600
    }
})
console.log(store,11111)
export default store;
